package com.flower.web.controller.business;

import cn.hutool.core.util.StrUtil;
import com.flower.common.annotation.Log;
import com.flower.common.core.controller.BaseController;
import com.flower.common.core.domain.AjaxResult;
import com.flower.common.core.page.TableDataInfo;
import com.flower.common.enums.BusinessType;
import com.flower.common.utils.poi.ExcelUtil;
import com.flower.system.domain.BizWiki;
import com.flower.system.service.IBizWikiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 花卉百科Controller
 * 
 * @author ruoyi
 * @date 2024-04-03
 */
@RestController
@RequestMapping("/system/wiki")
public class BizWikiController extends BaseController
{
    @Autowired
    private IBizWikiService bizWikiService;

    /**
     * 查询花卉百科列表
     */
    @PreAuthorize("@ss.hasPermi('system:wiki:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizWiki bizWiki)
    {
        startPage();
        List<BizWiki> list = bizWikiService.selectBizWikiList(bizWiki);
        return getDataTable(list);
    }

    /**
     * 导出花卉百科列表
     */
    @PreAuthorize("@ss.hasPermi('system:wiki:export')")
    @Log(title = "花卉百科", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizWiki bizWiki)
    {
        List<BizWiki> list = bizWikiService.selectBizWikiList(bizWiki);
        ExcelUtil<BizWiki> util = new ExcelUtil<BizWiki>(BizWiki.class);
        util.exportExcel(response, list, "花卉百科数据");
    }



    /**
     * 获取花卉百科详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:wiki:query')")
    @GetMapping(value = {"/{id}","/getWikiDetail"})
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(bizWikiService.selectBizWikiById(id));
    }

    /**
     * 新增花卉百科
     */
    @PreAuthorize("@ss.hasPermi('system:wiki:add')")
    @Log(title = "花卉百科", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizWiki bizWiki)
    {
        return toAjax(bizWikiService.insertBizWiki(bizWiki));
    }

    /**
     * 修改花卉百科
     */
    @PreAuthorize("@ss.hasPermi('system:wiki:edit')")
    @Log(title = "花卉百科", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizWiki bizWiki)
    {
        return toAjax(bizWikiService.updateBizWiki(bizWiki));
    }

    /**
     * 删除花卉百科
     */
    @PreAuthorize("@ss.hasPermi('system:wiki:remove')")
    @Log(title = "花卉百科", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(bizWikiService.deleteBizWikiByIds(ids));
    }

    /**
     * 查询所有 微信小程序用 用名称和
     */
    @PreAuthorize("@ss.hasPermi('system:wiki:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(BizWiki bizWiki)
    {
        String text = bizWiki.getSearchValue();
        //按照name和描述搜索
        BizWiki wiki = new BizWiki();
        if(StrUtil.isNotEmpty(text)){
            wiki.setName(text);
        }
        if(StrUtil.isNotEmpty(text)){
            wiki.setDescription(text);
        }
        //这两个条件用or连接 不分页
        startPage();
        List<BizWiki> list = bizWikiService.selectBizWikiList2(wiki);
        //list.forEach(System.out::println);
        //list.removeIf(Objects::isNull); // 过滤掉null元素
        //按照时间排序，最新的排在最前面
        //list.sort((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime()));
        list.forEach(i -> {
            if(StrUtil.isNotEmpty(i.getDescription())){
                i.setDescription(StrUtil.sub(i.getDescription(),0,100)+"......");
            }
        });

        return getDataTable(list);
    }

}
